import { createStore, applyMiddleware, combineReducers } from 'redux'

import { persistStore, persistReducer } from 'redux-persist'
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'
import thunk from 'redux-thunk'

import userReducer from './reducers/user'
import blogReducer from './reducers/blog'
import loginUserReducer from './reducers/loginUser'
import storage from 'redux-persist/lib/storage'

const persistConfig = {
	key: 'root',
	storage: storage,
	stateReconciler: autoMergeLevel2, // 查看 'Merge Process' 部分的具体情况
}

const reducer = combineReducers({
	user: userReducer,
	blog: blogReducer,
	loginUser: loginUserReducer,
})

const myPersistReducer = persistReducer(persistConfig, reducer)

const store = createStore(myPersistReducer, applyMiddleware(thunk))
/**
 * redux 的机制
 *  store 相当于是一个店长 reducer相当于是一个厨师  action相当于是服务员  组件相当于是顾客
 *    基本的逻辑：
 *      顾客想要点菜，需要先传递给服务员，服务员将信息传递给店长，  店长看了没问题之后给出事，  出事做好了再给店长  顾客就可能向店长要菜品了
 */

export const persistor = persistStore(store)

export default store
